Windows 7 ships with PowerShell 2.0 installed and
ready for action, so you can start scripting right away. Although that term generally means something like
“entering multiple statements or commands in a file that you can then
execute all at once,” things are a bit different in the PowerShell
world. What I mean is that when you’re at the PowerShell command line,
you can execute single commands, as you can using Command Prompt, but
you can also run through an entire script by executing multiple,
consecutive commands. For example, you can run one command that stores
data in a variable, and then you can run another command later on that
includes that variable.
So when you’re
executing commands in a PowerShell session, you’re essentially always
executing a script, even if it’s a script that contains only a single
command.
Starting a PowerShell Session
To get PowerShell up and running, select Start, type power,
and then select Windows PowerShell in the search results. Windows 7
starts a new PowerShell console session, and displays it in the Windows
PowerShell window, as shown in Figure 1.
As you can see, on the
surface PowerShell looks like a Command Prompt clone with its
character-based console and command-line prompt. In fact, not only does
PowerShell look
like Command Prompt, it also works
like Command Prompt. That is, almost anything you can do in a Command
Prompt session you can also do in exactly the same way in a PowerShell
session:
PowerShell supports the standard Command Prompt commands, including cd, del, dir, echo, md, move, ren, rm, rmdir, set, sort, and start.
PowerShell supports the standard DOSKEY techniques for recalling command lines and running multiple commands at once.
You can use PowerShell to start batch files, script, and programs.
PowerShell supports redirection (such as >) and piping (|).
You
can use PowerShell’s system menu (click the icon in the upper-left
corner of the window) to select, copy, and paste text, just as with the
Command Prompt window.
All this means that if
you’re comfortable working with Command Prompt, you won’t have much of a
learning curve when it comes to PowerShell (at least as far as the
above techniques are concerned).
Understanding PowerShell Cmdlets
What truly separates PowerShell from Command Prompt is the concept of the cmdlet
(pronounced “commandlet”), which is a PowerShell component that
encapsulates some larger functionality into a single item. (If you’re
familiar with Excel, a cmdlet is analogous to an Excel worksheet
function.) The name of each PowerShell cmdlet takes the following
general form:
Here, subject refers to some kind of object, and verb is some action that the cmdlet performs on that object. For example, you can use the Get-Host cmdlet to return information about the current PowerShell hosting environment, or you can use the Out-File cmdlet to output data to a file.
Although you can run
most cmdlets with no other text, most of the time you’ll augment a
cmdlet name with various parameters, depending on the cmdlet syntax. For
example, you use the Write-Host cmdlet
to write text to the hosting environment (which means the command line
in a PowerShell console session), so you need to specify the text you
want to write, as in this example:
write-host "Hello PowerShell World!"
I should also mention here that many cmdlets have alternate names called aliases that you can use. For example, the Get-Service cmdlet returns the current status of whatever service you specify, as in this example:
However, Get-Service also uses the alias gsv, the following command is equivalent:
Table 1 lists the PowerShell aliases
Table 1. Cmdlet Aliases Used By PowerShell
Alias | Cmdlet |
---|
% | ForEach-Object |
? | Where-Object |
ac | Add-Content |
asnp | Add-PSSnapIn |
cat | Get-Content |
cd | Set-Location |
chdir | Set-Location |
clc | Clear-Content |
clear | Clear-Host |
clhy | Clear-History |
cli | Clear-Item |
clp | Clear-ItemProperty |
cls | Clear-Host |
clv | Clear-Variable |
compare | Compare-Object |
copy | Copy-Item |
cp | Copy-Item |
cpi | Copy-Item |
cpp | Copy-ItemProperty |
ise | powershell_ise.exe |
iwmi | Invoke-WMIMethod |
kill | Stop-Process |
lp | Out-Printer |
ls | Get-ChildItem |
man | help |
md | mkdir |
measure | Measure-Object |
mi | Move-Item |
mount | New-PSDrive |
move | Move-Item |
mp | Move-ItemProperty |
mv | Move-Item |
nal | New-Alias |
ndr | New-PSDrive |
ni | New-Item |
nmo | New-Module |
nsn | New-PSSession |
nv | New-Variable |
cvpa | Convert-Path |
dbp | Disable-PSBreakpoint |
del | Remove-Item |
diff | Compare-Object |
dir | Get-ChildItem |
ebp | Enable-PSBreakpoint |
echo | Write-Output |
epal | Export-Alias |
epcsv | Export-Csv |
epsn | Export-PSSession |
erase | Remove-Item |
etsn | Enter-PSSession |
exsn | Exit-PSSession |
fc | Format-Custom |
fl | Format-List |
foreach | ForEach-Object |
ft | Format-Table |
fw | Format-Wide |
gal | Get-Alias |
gbp | Get-PSBreakpoint |
gc | Get-Content |
gci | Get-ChildItem |
gcm | Get-Command |
gcs | Get-PSCallStack |
gdr | Get-PSDrive |
ghy | Get-History |
ogv | Out-GridView |
oh | Out-Host |
popd | Pop-Location |
ps | Get-Process |
pushd | Push-Location |
pwd | Get-Location |
r | Invoke-History |
rbp | Remove-PSBreakpoint |
rcjb | Receive-Job |
rd | Remove-Item |
rdr | Remove-PSDrive |
ren | Rename-Item |
ri | Remove-Item |
rjb | Remove-Job |
rm | Remove-Item |
rmdir | Remove-Item |
rmo | Remove-Module |
rni | Rename-Item |
rnp | Rename-ItemProperty |
rp | Remove-ItemProperty |
rsn | Remove-PSSession |
rsnp | Remove-PSSnapin |
rv | Remove-Variable |
rvpa | Resolve-Path |
rwmi | Remove-WMIObject |
sajb | Start-Job |
gi | Get-Item |
gjb | Get-Job |
gl | Get-Location |
gm | Get-Member |
gmo | Get-Module |
gp | Get-ItemProperty |
gps | Get-Process |
group | Group-Object |
gsn | Get-PSSession |
gsnp | Get-PSSnapIn |
gsv | Get-Service |
gu | Get-Unique |
gv | Get-Variable |
gwmi | Get-WmiObject |
h | Get-History |
history | Get-History |
icm | Invoke-Command |
iex | Invoke-Expression |
ihy | Invoke-History |
ii | Invoke-Item |
ipal | Import-Alias |
ipcsv | Import-Csv |
ipmo | Import-Module |
ipsn | Import-PSSession |
sal | Set-Alias |
saps | Start-Process |
sasv | Start-Service |
sbp | Set-PSBreakpoint |
sc | Set-Content |
select | Select-Object |
set | Set-Variable |
si | Set-Item |
sl | Set-Location |
sleep | Start-Sleep |
sort | Sort-Object |
sp | Set-ItemProperty |
spjb | Stop-Job |
spps | Stop-Process |
spsv | Stop-Service |
start | Start-Process |
sv | Set-Variable |
swmi | Set-WMIInstance |
tee | Tee-Object |
type | Get-Content |
where | Where-Object |
wjb | Wait-Job |
write | Write-Output |
Note
Notice that all the standard Command Prompt commands—including cd, del, dir, echo, md, move, ren, rm, rmdir, set, sort, and start—are actually aliases of PowerShell cmdlets (Set-Location, Remove_Item, Get-ChildItem, and so on).